/* 
 *	Copyright (C) 2009-2010 miaohua
 *	http://code.google.com/p/lrcshower/
 *
 *  This Program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2, or (at your option)
 *  any later version.
 *   
 *  This Program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 *  GNU General Public License for more details.
 *   
 *  You should have received a copy of the GNU General Public License
 *  along with GNU Make; see the file COPYING.  If not, write to
 *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. 
 *  http://www.gnu.org/copyleft/gpl.html
 *
 */

#include "StdAfx.h"
#include <string>
#include "boost\regex.hpp"
#include "HelperFunctions.h"
#include "BaiduLrcParser.h"
using namespace std;
using namespace boost;

void CBaiduLrcParser::Parse(const tstring & Content)
{
     //Regex repression
     tregex Reg_Lrc(_T("<div class=\"BlueBG\"><strong>.*?</strong>(.+?)</div>.*?")
                    _T("<div.+?>(.+?)</div>.*?")
                    _T("<div.+?</div>.*?")
                    _T("<div.+?<div.+?<a href=\"(.+?)\".*?</div>.*?</div>"));
     tregex Reg_Diff(_T("</strong>(.+?)</a>"), boost::regex::icase|boost::regex::perl);
     tregex Reg_RemoveTag(_T("</?\\w+\\s*[^>]*>|amp;"), boost::regex::icase|boost::regex::perl);
     
     tsmatch Result;
     tsmatch SinAlbum;

     tstring::const_iterator bgn = Content.begin();
     tstring::const_iterator end = Content.end();

     // Clear list
     m_LrcList.clear();
     // Parse one by one
     while( regex_search(bgn, end, Result, Reg_Lrc) )
     {
            bgn = Result[0].second;
            LrcProperty Lrc;
            // Song Name
            Lrc.song = Result[1].str();
            Lrc.song = boost::regex_replace(Lrc.song,Reg_RemoveTag,_T(""));
            // Singer & Album
            tstring SingerAlbum = Result[2].str();
            tstring::const_iterator SubBgn = SingerAlbum.begin();
            tstring::const_iterator SubEnd = SingerAlbum.end();
            if( regex_search(SubBgn, SubEnd, SinAlbum, Reg_Diff) )
            { 
                SubBgn = SinAlbum[0].second;
                Lrc.singer = SinAlbum[1].str();
                if( regex_search(SubBgn, SubEnd, SinAlbum, Reg_Diff) )
                    Lrc.album = SinAlbum[1].str();
            }
                   
            // Parse singer(may be more than one singer) and remove tag
            Lrc.singer = boost::regex_replace(Lrc.singer,Reg_RemoveTag,_T(""));
            Lrc.album  = boost::regex_replace(Lrc.album,Reg_RemoveTag,_T(""));

            Lrc.url = Result[3].str();
            m_LrcList.push_back(Lrc);
     }
}

void CBaiduLrcParser::GetLrcList(LrcList & List)const
{
     // Clear all
     List.clear();
     // Add one by one
     for( unsigned int i = 0; i < m_LrcList.size(); ++i )
     {
          LrcProperty lrc;
          lrc.song = m_LrcList[i].song;
          lrc.singer = m_LrcList[i].singer;
          lrc.album = m_LrcList[i].album;
          lrc.url = m_LrcList[i].url;
          List.push_back(lrc);
     }
}

void CBaiduLrcNumParser::Parse(const tstring & Content)
{
     // Regex repression
     tregex Reg_LrcNum(_T("<!--resnum.*?([\\d,,]+)?-->"));
     tsmatch Num;
     if( regex_search(Content.begin(), Content.end(), Num, Reg_LrcNum) )
         m_TotalSongNum = FormatNum(Num[1].str());
     else
         m_TotalSongNum = 0;
}